import React from 'react';
import { connect } from 'dva';
import { PrimaryC, PrimaryGhostC, SecondHeaderC, FixedButtonC } from '../../../comps';
import PropTypes from 'prop-types';
import { Row, Col } from 'antd';
import moment from 'moment';
import FormDataC from './formDataC';
import ChangeDataC from './changeDataC';
import myStyle from './index.less';
import ScheduleTabe from './scheduleTabe'; //赎回计息
class BasicInfo extends React.Component {
  constructor() {
    super();
    this.state = {
      // 需要根据基本信息改变报表信息
      baseBaseInfoChangeReport: {
        // 发行价格
        parTotalValue: undefined,
        // 计息方式
        interestMethod: undefined,
        // 计息频率
        interestRate: undefined,
        // 计息基础
        interestBasis: undefined,
        // 起息日
        startDate: undefined,
        // 到期日
        endDate: undefined,
        // 票面利率
        fixedRate: undefined,
      },
    };
  }

  componentDidMount() {
    this.props.onRef(this);
    if (this.props.init) {
      this.setState({
        baseBaseInfoChangeReport: {
          parTotalValue: this.props.init.issueprice,
          interestMethod: this.props.init.twocycle,
          interestRate: this.props.init.interestFrequency,
          interestBasis: this.props.init.interestRules,
          startDate: this.props.init.interestDate,
          endDate: this.props.init.cashDueDate,
          fixedRate: this.props.init.couponRate,
        },
      });
    }
  }

  UNSAFE_componentWillReceiveProps(nextProps) {}

  // 还本付息相关字段更改基本信息
  onBaseInfoChange = (e) => {
    if (e.startDate) {
      e.startDate = moment(e.startDate).format('YYYY-MM-DD');
    }
    if (e.endDate) {
      e.endDate = moment(e.endDate).format('YYYY-MM-DD');
    }

    this.setState(
      {
        baseBaseInfoChangeReport: {
          ...this.state.baseBaseInfoChangeReport,
          ...e,
        },
      },
      () => {
        this.redemps.resetFxData(true);
      },
    );
  };

  //设置付息金额不可编辑的标志
  getFormData = (data) => {
    data &&
      data.map((item) => {
        if (moment(item.endDate) < moment()) {
          //到期日小于当前日期 则不可修改
          item.disable = 1;
        }
      });
    return data;
  };

  getInfo = () => {
    let query = this.formData.getInfo();
    return query;
  };

  redempsGetInfo = () => {
    let arr;
    this.redemps.getInfo((data) => {
      arr = data;
    });
    return arr;
  };

  render() {
    const { baseBaseInfoChangeReport } = this.state;
    const { form, loading, init, type } = this.props;
    return (
      <FixedButtonC
        style={{ borderTop: '1px solid rgba(67, 118, 255, 0.1)' }}
        topHtml={
          <div>
            <SecondHeaderC title='基础信息'></SecondHeaderC>
            <FormDataC type={type} init={init} onRef={(ref) => (this.formData = ref)} form={form} onBaseInfoChange={this.onBaseInfoChange} />
            <ScheduleTabe
              {...baseBaseInfoChangeReport}
              id={init.id}
              initInfo={this.getFormData(init?.zgtzptNonstandarCashFlows)}
              disableStatus={type == '5'}
              onRef={(ref) => (this.redemps = ref)}
            ></ScheduleTabe>
          </div>
        }
        bottomHtml={
          <div className={myStyle.outContainBox}>
            <PrimaryGhostC title='取消' onClick={() => this.props.onCancel()} style={{ marginRight: '10px' }} />
            <PrimaryC title='保存' onClick={() => this.props.onSave()} loading={loading} />
          </div>
        }
      ></FixedButtonC>
    );
  }
}

// props默认值
BasicInfo.propsTypes = {
  type: PropTypes.string || PropTypes.number, // 1-新建   2-编辑   5-查看
  init: PropTypes.object, // 基础信息
  onCancel: () => {}, // 取消
  onSave: () => {}, // 保存
};

export default connect(({ nonstandarmaintenanceNewM }) => ({
  nonstandarmaintenanceNewM,
}))(BasicInfo);
